import Taro, { Component, Config, getCurrentPages } from '@tarojs/taro'
import { View } from '@tarojs/components'
import { observer, inject, Image } from '@tarojs/mobx'
import '@tarojs/async-await'
import './index.scss'
import Stores from '../../stores/storeIdentifier'
import AuthenticationStore from '../../stores/authenticationStore'
import { AtButton } from 'taro-ui';
import WxOpenLoginModel from '../../models/login/wxOpenLoginModel';
import CartStore from '../../stores/cartStore';
import { ShoppingCartDto } from '../../services/cart/dto/shoppingCartDto';


export interface IIndexProps {
    authenticationStore: AuthenticationStore,
    cartStore: CartStore
}

@inject(Stores.AuthenticationStore, Stores.CartStore)
@observer
class Index extends Component<IIndexProps> {
    constructor(props: any) {
        super(props);
    }
    returnUrl: string
    accessToken: string
    cartItems: ShoppingCartDto[]
    config: Config = {
        navigationBarTitleText: '登录'
    };

    componentDidMount() {
        let pages = getCurrentPages()
        if (pages.length) {
            let fromUrl = pages[pages.length - 2]
            this.returnUrl = '/' + fromUrl.route
        }
        this.setState({})
    }

    wxOpenLoginModel: WxOpenLoginModel = new WxOpenLoginModel();
    gotoLogin = () => {
        Taro.navigateTo({
            url: '/pages/account/login/index'
        })
    }


    gotoWxOpenLogin = async (e) => {
        Taro.login({
        }).then(async (res) => {

            if (res.code) {
                // 发起网络请求
                this.wxOpenLoginModel.code = res.code;
                this.wxOpenLoginModel.state = 'Csproj_wxOpen_Bind';
                this.wxOpenLoginModel.returnUrl = this.returnUrl
                this.wxOpenLoginModel.encryptedData = e.detail.encryptedData;
                this.wxOpenLoginModel.iv = e.detail.iv;
                this.wxOpenLoginModel.rawData = e.detail.rawData;
                this.wxOpenLoginModel.signature = e.detail.signature;
                await this.login()
                if (this.accessToken) {
                    await this.backToCart()
                }
            }

        })
    }

    async login() {
        await this.props.authenticationStore!.wxOpenLogin(this.wxOpenLoginModel)
        this.accessToken = this.props.authenticationStore!.accessToken
        this.setState({})

    }

    async getMyShoppingCart() {
        await this.props.cartStore.getMyShoppingCart();
        this.cartItems = this.props.cartStore.cartItems;
        this.setState({})
    }

    async backToCart() {
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2];

        prevPage.setData({
            isAuthorize: true
        })
        Taro.navigateBack({
            delta: 1
        })
    }

    gotoRegister = () => {
        Taro.navigateTo({
            url: '/pages/account/register/index'
        })
    }

    render() {
        return (
            <View className='passport-wrap'>
                <View className='passport-bg'>
                    {/*<Image src={this.imageUrl}></Image>*/}
                </View>

                <View className='btn'>
                    {Taro.getEnv() === Taro.ENV_TYPE.WEAPP &&
                        <AtButton type='secondary' openType='getUserInfo' onGetUserInfo={this.gotoWxOpenLogin}>微信登录</AtButton>
                    }
                </View>
            </View>
        )
    }
}

export default Index
